import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Stack;


public class SemeskyUDPServer {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		DatagramSocket serverSocket = null;
		boolean running = true;
		Stack<String> mailbox=new Stack<String>(); //create stack to compare the last message.
		try {
			serverSocket = new DatagramSocket(9999);
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
		byte[] receiveData = new byte[1024];
        byte[] sendData = new byte[1024];
        
        while(running)
           {
              String lastMessage=null; //last message.
              if(mailbox.size()>0)
                   lastMessage=mailbox.pop(); //if the stack isn't empty take the last value from stack for last message. 
              DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
              serverSocket.receive(receivePacket);
              
              String incoming = new String(receivePacket.getData());
             // System.out.println(incoming);
              String[] split = incoming.split("#");
              String tc1 = split[0];
              String seq = split[1];//sequence numbers.
              int nextSeq = Integer.parseInt(seq) + 1;
              String msg = split[2];
              mailbox.push(msg); //push message on to stack
             // System.out.println(split[0]);
            //  System.out.println(split[1]);
            //  System.out.println(split[2]);
              
              
              DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss:SSSS");
		      Date date = new Date();		      
		      String ts = dateFormat.format(date);//time stamp
		      
		      
              
              
              //System.out.println("RECEIVED: " + sentence);
              
              InetAddress IPAddress = receivePacket.getAddress();
              int port = receivePacket.getPort();
              
              //String capitalizedSentence = sentence.toUpperCase();
              
              String packet = ts + "#" + tc1 + "#" + Integer.toString(nextSeq);
              //sendData = capitalizedSentence.getBytes();
              sendData = packet.getBytes();
              DatagramPacket sendPacket =
              new DatagramPacket(sendData, sendData.length, IPAddress, port);
              
              serverSocket.send(sendPacket);
              if(!msg.equals(lastMessage)) //if last message is the same as the msg coming in, dont' display.
              {
                  //"server then prints the following"
                  System.out.println("tc1(from client) = "+ split[0]);
                  System.out.println("ts(server time) = "+ ts);
                  System.out.println("Sequence number(from client) = " + seq);
                  System.out.println("Message(from client) = "+ msg);
                  System.out.println("========================================");
              }
           }
        
        serverSocket.close();
	}
	

}
